/*
 * Copyright (c) 2016. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.widget.layyer;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.com.dhc.danlu.R;


/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-07-26
 * Time: 10:51
 * Description: .....
 */
public class DefaultLayer {

    private LayerViewController mLayerViewController;

    /**
     * @param attachView 容器视图（must instance relativelayout or framelayout）
     * @param layerModel 加载模式 ( 预加载 :布局图层不用提前在xml 中定义好)
     */
    public DefaultLayer(ViewGroup attachView, LayerViewHelperImp.LayerModel layerModel) {
        if (null != attachView) {
            mLayerViewController = new LayerViewController(attachView, layerModel);
        }
    }

    public Context getContext() {
        if (null != mLayerViewController) {
            return mLayerViewController.getContext();
        }
        return null;
    }

    public LayerViewHelperImp.LayerStatus getStatus() {
        return mLayerViewController.getStatus();
    }


    public void showEmptyView(View emptyView) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        mLayerViewController.showVariableView(emptyView, LayerViewHelperImp.LayerStatus.empty);
    }

    public void showErrorView(View errorView) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        mLayerViewController.showVariableView(errorView, LayerViewHelperImp.LayerStatus.error);
    }

    public void showLoaddingView(View loaddingView) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        mLayerViewController.showVariableView(loaddingView, LayerViewHelperImp.LayerStatus.loadding);
    }


    public void showSuccessView(View view) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        mLayerViewController.showVariableView(view, LayerViewHelperImp.LayerStatus.success);
    }


    public void showEmptyView(String emptyTips, View.OnClickListener retryListener) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        View view = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
        TextView tv_empty = (TextView) view.findViewById(R.id.tv_error_info);
        if (!TextUtils.isEmpty(emptyTips)) {
            tv_empty.setText(emptyTips);
        }
        if (null != retryListener) {
            view.findViewById(R.id.btn_reload).setOnClickListener(retryListener);
        }
        mLayerViewController.showVariableView(view, LayerViewHelperImp.LayerStatus.empty);
    }

    public void showErrorView(String errorTips, View.OnClickListener retryListener) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        View view = LayoutInflater.from(context).inflate(R.layout.error_view, null);
        TextView tv_error = (TextView) view.findViewById(R.id.tv_error_info);
        if (!TextUtils.isEmpty(errorTips)) {
            tv_error.setText(errorTips);
        }
        if (null != retryListener) {
            view.findViewById(R.id.btn_error_retry).setOnClickListener(retryListener);
        }
        mLayerViewController.showVariableView(view, LayerViewHelperImp.LayerStatus.error);
    }

    public void showLoaddingView(String loadTips) {
        Context context = getContext();
        if (null == context) {
            return;
        }
        View view = LayoutInflater.from(context).inflate(R.layout.loading_view, null);
        TextView tv_load = (TextView) view.findViewById(R.id.tv_loadding);
        if (!TextUtils.isEmpty(loadTips)) {
            tv_load.setText(loadTips);
        }
        mLayerViewController.showVariableView(view, LayerViewHelperImp.LayerStatus.loadding);
    }

    public void releaseRef() {
        mLayerViewController.releasRef();
    }

}